<template>
  <!-- 根标签 -->
  <div>
    <!-- 标题1：退货商品基本信息 -->
    <el-card style="border: 2px; background-color: #ffffff">
      <div class="title-content" style="margin-left: 20px; margin-top: 20px;">
        <i class="el-icon-menu" style="font-size: 20px"></i>
        <span style="font-size: 20px">退货商品</span>
      </div>
      <!-- 打印退货商品的基本信息 -->
      <div>
        <el-table :data="tableData" style="width: 100%;margin-bottom: 20px;" row-key="id" default-expand-all  :header-cell-style="{ background: '#f5f7fa', color: '#333', fontWeight: 'bold', textAlign: 'center' }"
                  :cell-style="{ textAlign: 'center' }">
          <!-- 展示商品图片 -->
          <el-table-column prop="productImage" label="商品图片">
            <template slot-scope="scope">
              <img :src="scope.row.productImage" style="width: 100px; height: 100px;" alt="商品图片">
            </template>
          </el-table-column>
          <!-- 展示商品名称 -->
          <el-table-column prop="productName" label="商品名称"></el-table-column>
          <!-- 展示商品价格 -->
          <el-table-column prop="product.price" label="商品价格"></el-table-column>
          <!-- 展示商品描述 -->
          <el-table-column prop="product.description" label="商品描述"></el-table-column>
          <!-- 展示商品退货数量 -->
          <el-table-column prop="refundQuantity" label="退货数量"></el-table-column>
          <!-- 展示小计 -->
          <el-table-column label="小计">
            <template slot-scope="scope">
              <!-- 显示出订单数量和商品价格的乘积 -->
              {{'￥' +scope.row.refundQuantity * scope.row.product.price}}
            </template>
          </el-table-column>
        </el-table>
        <!-- 展示合计 -->
        <span style="margin-left: 1000px;color: red">合计：￥{{ getTotalRefundAmount() }}</span>
      </div>
    </el-card>

    <!-- 标题2：服务单信息 -->
    <el-card>
      <div class="service-info">
        <div class="title-content">
          <i class="el-icon-s-claim" style="font-size: 20px"></i>
          <span style="font-size: 20px">服务单信息</span>
        </div>
        <div class="table-container">
          <!-- 第一行：服务单号 -->
          <el-row :gutter="20" class="table-row">
            <el-col :span="6" class="table-cell-label">服务单号</el-col>
            <el-col :span="18" class="table-cell-value">{{ orderRefund.id }}</el-col>
          </el-row>
          <!-- 第二行：申请状态 -->
          <el-row :gutter="20" class="table-row">
            <el-col :span="6" class="table-cell-label">申请状态</el-col>
            <el-col :span="18" class="table-cell-value">
              <!-- 申请状态:0待审核 1退款中 2退款成功 3拒绝  -->
              <span v-if="orderRefund.status === 0">待审核</span>
              <span v-else-if="orderRefund.status === 1">退款中</span>
              <span v-else-if="orderRefund.status === 2">退款成功</span>
              <span v-else-if="orderRefund.status === 3">拒绝</span>
              <span v-else>状态异常</span>
            </el-col>

          </el-row>
          <!-- 第三行：订单编号和超链接 -->
          <el-row :gutter="20" class="table-row">
            <el-col :span="6" class="table-cell-label">订单号</el-col>
            <el-col :span="18" class="table-cell-value">
              <el-link type="primary" @click="viewOrder">{{ orderRefund.order.orderNo }} 查看</el-link>
            </el-col>
          </el-row>
          <!-- 第四行：申请时间 -->
          <el-row :gutter="20" class="table-row">
            <el-col :span="6" class="table-cell-label">申请时间</el-col>
            <el-col :span="18" class="table-cell-value">{{ orderRefund.createdAt }}</el-col>
          </el-row>
          <!-- 第五行：用户账号 -->
          <el-row :gutter="20" class="table-row">
            <el-col :span="6" class="table-cell-label">用户账号</el-col>
            <el-col :span="18" class="table-cell-value">{{ orderRefund.user.username }}</el-col>
          </el-row>
           <!--第六行：联系人 -->
          <el-row :gutter="20" class="table-row">
            <el-col :span="6" class="table-cell-label" style="color:red">联系人</el-col>
            <el-col :span="18" class="table-cell-value">{{ serviceInfo.contactPerson }}</el-col>
          </el-row>
          <!-- 第七行：联系电话 -->
          <el-row :gutter="20" class="table-row" style="color:red">
            <el-col :span="6" class="table-cell-label">联系电话</el-col>
            <el-col :span="18" class="table-cell-value">{{ orderRefund.phone }}</el-col>
          </el-row>
          <!-- 第八行：退货原因 -->
          <el-row :gutter="20" class="table-row">
            <el-col :span="6" class="table-cell-label">退货原因</el-col>
            <el-col :span="18" class="table-cell-value">{{ orderRefund.reason }}</el-col>
          </el-row>
           <!--第九行：问题描述-->
          <el-row :gutter="20" class="table-row" style="color:red">
            <el-col :span="6" class="table-cell-label">问题描述</el-col>
            <el-col :span="18" class="table-cell-value">{{ serviceInfo.description }}</el-col>
          </el-row>
           <!--第十行：凭证图片-->
          <el-row :gutter="20" class="table-row" style="color:red">
            <el-col :span="6" class="table-cell-label">凭证图片</el-col>
            <el-col :span="18" class="table-cell-value">
              <!-- 设置图片上传功能 -->
              <el-upload
                class="upload-demo"
                drag
                action=""
                :http-request="uploadFile"
                :on-change="handleChange"
                multiple>
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                <!-- 在这里展示上传的图片 -->
                <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
              </el-upload>
            </el-col>
            // 这里展示上传的图片
            <el-col :span="18" class="table-cell-value">
              <el-image :src="imageUrl1" style="width: 100px; height: 100px;"></el-image>
            </el-col>
            <el-col style="color:red; margin-top: 30px">
              <span>注:这里的上传图片业务逻辑有问题，不应该是后台管理员上传凭证图片，仅为功能演示</span>
            </el-col>
          </el-row>
        </div>
      </div>
    </el-card>


  <!-- 标题3：订单信息 -->
    <el-card>
      <template>
        <div class="refund-information">
          <div class="title-content">
            <i class="el-icon-folder" style="font-size: 20px"></i>
            <span style="font-size: 20px">订单信息</span>
          </div>
          <div class="table-container">
            <!-- 第一行：订单金额 -->
            <el-row :gutter="20" class="table-row">
              <el-col :span="8" class="table-cell-label">订单金额</el-col>
              <el-col :span="16" class="table-cell-value">
                <span>￥{{ getTotalRefundAmount()}}</span>
              </el-col>
            </el-row>
            <!-- 第二行：确认退款金额 -->
            <el-row :gutter="20" class="table-row">
              <el-col :span="8" class="table-cell-label">确认退款金额</el-col>
              <el-col :span="16" class="table-cell-value">
                <span>￥{{ getTotalRefundAmount() }}</span>
              </el-col>
            </el-row>

            <!-- 第三行：选择收货点 -->
            <el-row :gutter="20" class="table-row" style="color:red">
              <el-col :span="8" class="table-cell-label">选择收货点</el-col>
              <el-col :span="16" class="table-cell-value">
                <el-select v-model="selectedDeliveryPoint" placeholder="请选择" style="margin-left: 30px">
                  <el-option
                    v-for="(point, index) in deliveryPoints"
                    :key="index"
                    :label="point"
                    :value="point"
                  ></el-option>
                </el-select>
              </el-col>
            </el-row>
            <!-- 第四行：收货人姓名 -->
            <el-row :gutter="20" class="table-row">
              <el-col :span="8" class="table-cell-label">收货人姓名</el-col>
              <el-col :span="16" class="table-cell-value">
                {{orderRefund.user.username}}
              </el-col>
            </el-row>
            <!-- 第五行：所在区域 -->
            <el-row :gutter="20" class="table-row" style="color:red">
              <el-col :span="8" class="table-cell-label">所在区域</el-col>
              <el-col :span="16" class="table-cell-value">

              </el-col>
            </el-row>
            <!-- 第六行：详细地址 -->
            <el-row :gutter="20" class="table-row" style="color:red">
              <el-col :span="8" class="table-cell-label">详细地址</el-col>
              <el-col :span="16" class="table-cell-value">

              </el-col>
            </el-row>
            <!-- 第七行：联系电话 -->
            <el-row :gutter="20" class="table-row" style="color:red">
              <el-col :span="8" class="table-cell-label">联系电话</el-col>
              <el-col :span="16" class="table-cell-value">

              </el-col>
            </el-row>
          </div>
        </div>
      </template>

    </el-card>
  </div>
</template>

<script>
import { findAll, uploadPicture } from '@/api/order/refundDetail'
import { getRowsByRefundDetail } from '@/utils/auth'

export default {
  name: 'refundDetail',
  data() {
    return {
      // 接收refundManagerV2传过来的参数
      orderRefund: this.$route.query.orderRefund,
      tableData: [],
      imageUrl1: null,
      file: null,
      deliveryPoints: ['顺义仓', '高新仓', '朝阳仓'],
      selectedDeliveryPoint: null,
      refundAmount:null,
      serviceInfo: {
        serviceNumber: '3',
        status: '已完成',
        orderNumber: '201809150101000001',
        applyTime: '2018-10-17 14:34:57',
        username: 'test',
        contactPerson: '大梨',
        phone: '18000000000',
        returnReason: '质量问题',
        description: '老是卡',
      },
    };
  },
  methods: {
    handleChange(file){
      this.file = file.raw;
    },
    fetchOrderRefund() {
      // 首先尝试从路由查询参数中获取
      if (this.$route.query.orderRefund) {
        this.orderRefund = this.$route.query.orderRefund;
      } else {
          this.orderRefund = getRowsByRefundDetail(); // 或者设置为null、默认值等
      }
    },
    uploadFile(){
      const file = this.file;
      if (!file) {
        this.$message.error("请选择一个文件");
        return;
      }
      const formData = new FormData();
      formData.append('file', file);

      const refundId = this.orderRefund.id; // 确保 refundId 有值
      if (!refundId) {
        this.$message.error("请提供退款ID");
        return;
      }
      formData.append('refundId', refundId); // 将 refundId 添加到 FormData 中
      uploadPicture(formData).then((res) => {
        this.$message({
          message: "上传成功",
          type: "success",
        });
        this.fileSrc = res.data
        console.log("fileSrc",this.fileSrc)
        this.imageUrl1 = this.fileSrc  // 上传成功后展示图片
      }).catch((error) => {
        this.$message.error("上传失败");
      });
    },
    getTotalRefundAmount() {
      return this.tableData.reduce((total, item) => {
        return total + item.refundQuantity * item.product.price;
      }, 0);
    },
    findAll(){
        // 如果传递过来的参数为null，则从vuex中获取数据
        const refundDetailId = this.orderRefund.id;
        findAll(refundDetailId)
          .then(res => {
            this.tableData = res.data;
          })
          .catch(error => {
            this.$message.error("查询失败");
          });

    },
    viewOrder() {
      // alert("暂时未设置!")
      // this.$router.push(`/order/${this.serviceInfo.orderNumber}`);
    },
  },
  created() {
    this.findAll();
    this.fetchOrderRefund();
  }
}
</script>

<style>
.service-info {
  padding: 20px;
}

.table-container {
  max-width: 600px;
}

/* 表格行样式 */
.table-row {
  border: 1px solid #ddd; /* 外边框 */
  overflow: hidden; /* 防止子元素溢出 */
}

/* 表格单元格 */
.table-cell-label {
  background-color: #f8f8f8; /* 标题列背景色 */
  padding: 10px; /* 内间距 */
  border-right: 1px solid #ddd; /* 右边框 */
  font-weight: bold; /* 加粗标题 */
  text-align: left; /* 左对齐 */
}

.table-cell-value {
  padding: 10px; /* 内间距 */
  border-right: 1px solid #ddd; /* 右边框 */
  text-align: left; /* 左对齐 */
}

/* 表格的最后一列不显示右边框 */
.table-cell-label:last-child {
  border-right: none;
}

.table-cell-value:last-child {
  border-right: none;
}

/* 超链接样式 */
.el-link {
  color: #409eff; /* 链接颜色 */
  cursor: pointer;
}

/* 图片样式 */
.el-image {
  margin-right: 10px;
  display: inline-block; /* 确保图片在同一行 */
}
</style>
